<?php
/**
 * Created by PhpStorm.
 * User: zgscwjm
 * Date: 2017/2/15
 * Time: 下午1:47
 */

namespace Home\Controller;
use Think\Controller;

class MessageController extends Controller
{


    public function __construct()
    {
        parent::__construct();
        if (is_null($_SESSION["USER"])) {
            echo '<script> alert("请先登录")</script>';
//            $this->redirect("/Login/login");
            $this->display("Web/login");
        }
    }
    /* 初始化 IM 对话框 */
    public function ImIndex(){
        $this->display("Web/im");
    }

    public function sendMessage(){
        $data['fid'] = $_SESSION['USER']['id'];
        $data['tid'] = $_POST['id'];
        $data['message']=$_POST['msg'];
        $data['type']=0;
        if(M('message')->add($data))
        {
            $this->ajaxReturn(array('status'=>1));
        }else{
            $this->ajaxReturn(array('status'=>2));
        }

    }

    /*public function getMessageList(){
        $id = $_SESSION['USER']['id'];
        $list = M('message')->where('fid = "'.$id.'"')->group('tid')->select();
        foreach($list as &$item){
            $item['nickname'] = $this->getUserNameById($item['tid']);
            $item['count']= $this->getUnReadCount($item['tid'],$id);
        }

        $this->ajaxReturn($item);

    }*/
    public function getMessageList(){
        $id = $_SESSION['USER']['id'];
        $list = M('message')->where('fid = "'.$id.'"')->group('tid')->select();
        foreach($list as &$item){
            $item['nickname'] = $this->getUserNameById($item['tid']);
            $item['count']= $this->getUnReadCount($id,$item['tid']);
        }

        $this->ajaxReturn($list);

    }

    public function getUnReadCount($tid,$fid){
       return M('message')->where('type = 0 and tid = "'.$tid.'" and fid = "'.$fid.'"')->count();
    }

    public function getUserNameById($id){
        $user = M('userinfo')->where('id = "'.$id.'"')->find();
        if ($user) {
            return $user['nickname'];
        }else{
            return "游客";
        }
    }

    public function getAllMessage(){
        $mid = $_SESSION['USER']['id'];
        $id = I('id');
        $list = M('message')->where('(tid = "'.$mid.'" and fid = "'.$id.'") or ('.'fid = "'.$mid.'" and tid = "'.$id.'")')->order("id asc")->select();
        M('message')->where('type = 0 and tid = "'.$mid.'" and fid = "'.$id.'"')->save(array('type'=>1));
        $this->ajaxReturn($list);
    }

    public function getUnReadMessage(){
        $id = I('id');
        $changeType = I('changeType');
        $mid = $_SESSION['USER']['id'];
        $list = M('message')->where('type = 0 and tid = "'.$mid.'" and fid = "'.$id.'"')->select();
        if($changeType==1){
            M('message')->where('type = 0 and tid = "'.$mid.'" and fid = "'.$id.'"')->save(array('type'=>1));
        }
        $result = array(
            'data' => $list,
            'count' => count($list)
        );
        $this->ajaxReturn($result);
    }

    public function getUnReadMessageCount(){
        $mid = $_SESSION['USER']['id'];
        $count = M('message')->where('type = 0 and tid = '.$mid)->count();
        $this->ajaxReturn($count);
    }

}